from sqlalchemy import PrimaryKeyConstraint, Sequence
from sqlalchemy.dialects.postgresql import TIMESTAMP

from tzq.extensions import db


class Message(db.Model):
    """ 国际化表 """
    __tablename__ = 'dm_message_t'
    __table_args__ = (
        # 主键约束
        PrimaryKeyConstraint('message_id', name='dm_message_pk', comment='主键约束'),
        # 表注释
        {'comment': '国际化表'},
    )
    message_id = db.Column(
        db.BigInteger,
        Sequence('dm_message_s'),
        primary_key=True,  # 主键
        server_default=Sequence('dm_message_s').next_value(),
        nullable=False,  # 非空约束。False为禁止为空
        comment='主键ID'
    )
    key = db.Column(db.String(1000), nullable=False, comment='提示信息key')
    value = db.Column(db.String(1000), nullable=False, comment='提示信息内容')
    language = db.Column(db.String(50), nullable=False, comment='语种')
    principal = db.Column(db.String(100), nullable=False, comment='责任人：HW工号')
    online_date = db.Column(TIMESTAMP(timezone=False, precision=0), nullable=False,
                            server_default=db.func.current_date(), comment='上线时间')
    comment = db.Column(db.String(3000), comment='备注')
    delete_flag = db.Column(db.String(10), nullable=False, server_default='N',
                            comment='删除标识：记录该记录是否删除，Y=>删除、N=>未删除')
    created_by = db.Column(db.String(50), nullable=False, server_default='0.0.0.0',
                           comment='创建人：创建人的客户端IP地址')
    creation_date = db.Column(TIMESTAMP(timezone=False, precision=0), nullable=False,
                              server_default=db.func.clock_timestamp(), comment='创建时间')
    last_updated_by = db.Column(db.String(50), nullable=False, server_default='0.0.0.0',
                                comment='最后更新人：更新人的客户端IP地址')
    last_update_date = db.Column(TIMESTAMP(timezone=False, precision=0), nullable=False,
                                 server_default=db.func.clock_timestamp(), comment='最后更新时间')

    def to_dict(self):
        return {
            'message_id': self.message_id,
            'key': self.key,
            'value': self.value,
            'language': self.language,
            'principal': self.principal,
            'online_date': self.online_date.strftime('%Y-%m-%d'),
            'comment': self.comment,
            'delete_flag': self.delete_flag,
            'created_by': self.created_by,
            'creation_date': self.creation_date.strftime('%Y-%m-%d %H:%M:%S'),
            'last_updated_by': self.last_updated_by,
            'last_update_date': self.last_update_date.strftime('%Y-%m-%d %H:%M:%S'),
        }
